package.cpath = "luaclib/?.so;3rd/lua-cjson/?.so;"
package.path = "lualib/?.lua;3rd/json/?.lua;shao/?.lua"
local skynet = require "skynet"
local mc = require "multicast"
local socket = require "socket"
local string = require "string"
local websocket = require "websocket"
local httpd = require "http.httpd"
local urllib = require "http.url"
local sockethelper = require "http.sockethelper"
local json = require "cjson"
json.encode_sparse_array(true)

local packdeal = {}

function packdeal.send_package(id, ack, room_id, uid)
    local data = json.encode(ack)
    print('-----ack_pack----',data)
    if room_id ~= nil and ack.cmd ~= 3001 then 
        skynet.error("-- packdeal --", room_id, "-- send to uid:" .. uid, "-------> data:" .. data)
    else
        if ack.cmd ~= 6 and ack.cmd ~= 62 and ack.cmd ~= 3001 then
            -- skynet.error("send:===" .. data)
        end
    end

    local frame = string.pack("B", 128 | 1)
    local l = #data

    local mask_bit = 0
    if l < 126 then
        frame = frame .. string.pack("B", l | mask_bit)
    elseif l < 65535 then
        frame = frame .. string.pack(">BH", 126 | mask_bit, l)
    else
        frame = frame .. string.pack(">BL", 127 | mask_bit, l)
    end
    frame = frame .. data
    return socket.write(id, frame)
end
function packdeal.unpack_package(text, room_id, uid)
    if text == '@heart' then
        return nil
    end
    if string.len(text) > 3 then
        if room_id ~= nil then 
            skynet.error("-- packdeal --", room_id, "-- receive uid:" .. uid, "-------> text:" .. text)
        elseif uid ~= nil then
            if text ~= '{"cmd":5,"rs":1}' and text ~= '{"cmd":5,"rs":2}' and text ~= '{"cmd":5}' then
                skynet.error("receive:==uid==== CMD:",uid, json.decode(text).cmd, text)
            end
        end
        return json.decode(text)
    end
end

function packdeal.writelog(file, text)
    if file then
        local date = os.date("%m-%d");
        -- os.execute("mkdir /skynet/log/"..date)
        local file2 = assert(io.open("/skynet/log/" .. date .. '/' .. file, "a"))
        file2:write(text .. "\n")
        file2:close()
    end
end

function packdeal.writelog_error(file, text)
    if file then
        local date = os.date("%m-%d");
        -- os.execute("mkdir /skynet/log/"..date)
        local file2 = assert(io.open("/skynet/log/" .. date .. '/' .. file, "a"))
        file2:write(text .. "\n")
        file2:close()
    end
end

function packdeal.vdump(obj)
    skynet.error(json.encode(obj))
end

return packdeal